let jwt = require('jwt-simple');
// const { now } = require('mongoose');

const jwtEncode = (adminName,secret)=>{
    // console.log(adminName);
    return jwt.encode({
        adminName,
        exp: Date.now() /1000 + 1000//可传可写死
    },secret)
}
// console.log(adminName);

const jwtDecode = (token,secret)=>{
    try{
       const {
        adminName
       } = jwt.decode(token,secret) 
       return {
        code: 1,
        adminName
       }
    }catch (e){
        return {
            code: 0
        }
    }
}

module.exports = {
    jwtEncode,
    jwtDecode
}





// const username = "laowang"
// const secret = "12335678"

// //生成token
// const token = jwt.encode({
//     //基于一个值生成token
//     username,
//     //设置token生效时间,此包是秒的时间戳
//     exp: Date.now()/1000 + 500//500秒过期
// },secret)

// console.log("token",token);

// //token是base64编码,可以解码

// /*
// eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.  
//   -解码{"typ":"JWT","alg":"HS256"}
//    当前token的相关信息
// eyJ1c2VybmFtZSI6Imxhb3dhbmciLCJleHAiOjE2NTY1ODgxNzMuMTExfQ.
//   -解码{"username":"laowang","exp":1656588173.111}
//    荷载信息
// a-wfycPuKAjWMsRqCBcQt23y0e8CTkfdQPN6wLHRa5k
//   -kp5ĭۼ{P<ް,tZ怀
//    加密串需要的凭证,也叫签名,无法解码,拿到真正的secret值和当前密文才能解码
// */


// const decoded = jwt.decode(token, secret);

// console.log("decoded",decoded);


